<template>
  <div class="app-container">
    <el-card class="filter-container">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <div class="filter-btn">
          <el-button
            icon="el-icon-search"
            type="primary"
            size="small"
            @click="handleSearchList()"
          >
            查询
          </el-button>
          <el-button
            icon="el-icon-setting"
            size="small"
            @click="handleResetSearch()"
          >
            重置
          </el-button>

          <el-button
            class="btn-add"
            type="success"
            size="mini"
            icon="el-icon-plus"
            @click="add"
          >
            添加
          </el-button>
        </div>

      </div>
      <div style="margin-top: 25px">
        <el-form :inline="true" :model="listQuery" size="small">
          <el-row>
            <el-col :sm="12" :lg="6">
              <el-date-picker
                v-model="searchDate"
                value-format="yyyy-MM-dd"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['12:00:00']"
              />
            </el-col>
          </el-row>

        </el-form>
      </div>
    </el-card>

    <el-card class="table-container">
      <el-table
        v-loading="listLoading"
        element-loading-text="加载中"
        :data="list"
        fit
      >
        <el-table-column align="center" width="200" property="date" label="日期" />
        <el-table-column align="center" width="200" property="title" label="标题" />
        <el-table-column align="center" label="心情">
          <template slot-scope="scope">
            <span>
              <el-rate
                v-model="scope.row.mood"
                show-text
              />
            </span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="operate-cell">
          <template slot-scope="scope">

            <el-button
              size="mini"
              type="success"
              @click="update(scope.row)"
            >查看
            </el-button>

          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageLimit" @pagination="getList" />
    </el-card>

  </div>
</template>

<script>

import {
  getPageList
} from '@/api/life/diary'

import Pagination from '@/components/Pagination'

const defaultListQuery = {
  pageNum: 1,
  pageLimit: 10,
  sorts: '-date'
}

export default {
  components: { Pagination },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,

      searchDate: null,

      listLoading: true
    }
  },
  watch: {
    searchDate: function(newValue) {
      if (newValue != null) {
        this.listQuery.startTime = newValue[0]
        this.listQuery.endTime = newValue[1]
      } else {
        this.listQuery.startTime = null
        this.listQuery.endTime = null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getPageList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearchList() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    add() {
      this.$router.push({
        path: '/life/days/diary-add'
      })
    },
    update(row) {
      this.$router.push({
        path: '/life/days/diary-update',
        query: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style scoped>
  .el-image{
    width: 40px;
    height: 40px;
  }
</style>
